/*----------------------------------------------------------------------------
 *      RL-ARM - TCPnet
 *----------------------------------------------------------------------------
 *      Name:    FTP_UIF.C
 *      Purpose: FTP Server User Interface Module
 *      Rev.:    V4.70
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2013 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

#include <Net_Config.h>
#include <File_Config.h>
#include "gtfs.h"
/*----------------------------------------------------------------------------
 * FTP Server File Access Functions
 *---------------------------------------------------------------------------*/
 void chk_name(U8 *name)
 {
	U8 i;	
	for(i=0;i<32;i++)
	{
		if(name[i]== 0 )break;
		if(name[i]=='*'){name[i]=0;break;}
		if(name[i]=='\\')name[i]='/';
	}	 
 }

/*--------------------------- ftp_fopen -------------------------------------*/

void *ftp_fopen (U8 *fname, U8 *mode) {
  /* Open file 'fname' for reading or writing. Return file handle. */
  FHANDLE fh;
  chk_name(fname);
  fh=FOPEN ((const char *)fname, (const char *)mode);
  return ((void*)fh);
}


/*--------------------------- ftp_fclose ------------------------------------*/

void ftp_fclose (void *file) {
  /* Close the file opened for reading or writing. */
  FCLOSE ((FHANDLE)file);
}


/*--------------------------- ftp_fread -------------------------------------*/

U16 ftp_fread (void *file, U8 *buf, U16 len) {
  /* Read 'len' bytes from file to buffer 'buf'. The file will be closed, */
  /* when the number of bytes read is less than 'len'. */
  return (FREAD (buf, 1, len, (FHANDLE)file));
}


/*--------------------------- ftp_fwrite ------------------------------------*/

U16 ftp_fwrite (void *file, U8 *buf, U16 len) {
  /* Write 'len' bytes from buffer 'buf' to a file. */
  return (FWRITE (buf, 1, len, (FHANDLE)file));
}


/*--------------------------- ftp_fdelete -----------------------------------*/

BOOL ftp_fdelete (U8 *fname) {
  /* Delete a file, return __TRUE on success. */
  chk_name(fname);
  if (FRM((char *)fname) == 0) {
    return (__TRUE);
  }
  return (__FALSE);
}


/*--------------------------- ftp_frename -----------------------------------*/

BOOL ftp_frename (U8 *fname, U8 *newn) {
  /* Rename a file, return __TRUE on success. */
  chk_name(fname);
  chk_name(newn);	
  if (FREN((char *)fname, (char *)newn) == 0) {
    return (__TRUE);
  }
  return (__FALSE);
}


/*--------------------------- ftp_ffind -------------------------------------*/
U16 ftp_ffind (U8 code, U8 *buf, U8 *mask, U16 buflen) {
  /* This function is called by the FTP server to find file names and other */
  /* file information. The output data is stored in ascii format to output  */
  /* buffer 'buf' Parameter 'code' specifies requested file information.    */
  /* Values for 'code':                                                     */
  /*    0 - read file size                                                  */
  /*    1 - read last-modified time of a file                               */
  /*    2 - list file names only (first call)                               */
  /*    3 - list file directory in extended format (first call)             */
  /*    4 - list file names only (repeated call)                            */
  /*    5 - list file directory in extended format (repeated call)          */
  chk_name(mask);	
  return FFIND(code, buf, mask, buflen);
}


/*--------------------------- ftp_accept_host -------------------------------*/
#if 0
BOOL ftp_accept_host (U8 *rem_ip, U16 rem_port) {
  /* This function checks if a connection from remote host is accepted or  */
  /* not. If this function is missing, all remote hosts are accepted.      */

   if (rem_ip[0] == 192  &&
       rem_ip[1] == 168  &&
       rem_ip[2] == 1    &&
       rem_ip[3] == 1) {
      /* Accept a connection. */
      return (__TRUE);
   }
   /* Deny a connection. */
   return (__FALSE);
}
#endif

/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/
